package com.wntms.auser.exceptions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wntms.ywa.pojo.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        Result result = new Result(401, "认证失败", null);

        response.setContentType("application/json;charset=utf-8");
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(result);
        response.getWriter().write(json);
    }
}
